<?php
class ControllerModuleAlsoBought extends Controller {
	protected function index($setting) {
		
      	$this->data['heading_title'] = $this->config->get('also_bought_heading_title_'. $this->config->get('config_language_id'));
		
		$this->data['button_cart'] = $this->language->get('button_cart');
				
		$this->load->model('module/also_bought');
		
		$this->load->model('tool/image');
		
		$this->data['show_add_to_cart_button'] = $setting['button_cart'];
		
		$this->data['products'] = array();
		
		if (isset($this->request->get['product_id'])){
		
			$data = array(
				'dc_period'            => $this->config->get('also_bought_dc_period') * 60 * 60,
				'filter_product_id'    => $this->request->get['product_id'],
				'order_status_id'      => implode(",", $this->config->get('also_bought_allowed_statuses')),
				'sort'  			   => $setting['sort'],
				'order' 			   => $setting['sort_type'],
				'start' 			   => 0,
				'limit' 			   => $setting['limit']
			);

			$results = $this->model_module_also_bought->getAlsoBoughtProducts($data);
			
			if ($results){
				foreach ($results as $result) {
					if ($result['image']) {
						$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
					} else {
						$image = false;
					}
								
					if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
						$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
					} else {
						$price = false;
					}
							
					if ((float)$result['special']) {
						$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
					} else {
						$special = false;
					}
					
					if ($this->config->get('config_review_status')) {
						$rating = $result['rating'];
					} else {
						$rating = false;
					}
                    //get product category info 01.20.2015
                    $category_to_product_info =  $this->model_catalog_product->getCategories($result['product_id']);
                    $category_id_to_product = $category_to_product_info[1]['category_id'];
                    $product_catelog_info =  $this->model_catalog_category->getCategory($category_id_to_product);
                    $product_catelog_name = $product_catelog_info['name'];
                    //end
					$this->data['products'][] = array(
                        'product_id' => $result['product_id'],
                        'thumb'   	 => $image,
                        'name'    	 => $result['name'],
                        'price'   	 => $price,
                        'special' 	 => $special,
                        'rating'     => $rating,
                        'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
                        'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
                        'product_classify' => $product_catelog_name,
                        'tag'        => $result['tag']
					);
				}
			}
		}
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/also_bought.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/also_bought.tpl';
		} else {
			$this->template = 'default/template/module/also_bought.tpl';
		}

		$this->render();
	}
}
?>